package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.UservipService;
import com.youi.entity.Uservip;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 用户会员信息控制层
* @author Administrator
*/
@Controller
public class UservipController  extends HttpRequestManage{

		@Autowired
		private UservipService uservipService;

		/**
		 *添加或编辑用户会员信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditUservip")
		public String addOrEditUservip(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Uservip uservip=null;
			if (id!=null) {
				uservip=uservipService.get(id);
			}else {
				uservip=new Uservip();
				uservip.setDelStatus(DelStatus.NODEL);
				uservip.setGmtCreated(new Date());
			}
			Long userId=optLong("userId",request);
			if (userId!=null){
				uservip.setUserId(userId);
			}
			String nickname=optString("nickname",request);
			if (nickname!=null){
				uservip.setNickname(nickname);
			}
			String gender=optString("gender",request);
			if (gender!=null){
				uservip.setGender(gender);
			}
			String avatarUrl=optString("avatarUrl",request);
			if (avatarUrl!=null){
				uservip.setAvatarUrl(avatarUrl);
			}
			String country=optString("country",request);
			if (country!=null){
				uservip.setCountry(country);
			}
			String province=optString("province",request);
			if (province!=null){
				uservip.setProvince(province);
			}
			String city=optString("city",request);
			if (city!=null){
				uservip.setCity(city);
			}
			Long accountId=optLong("accountId",request);
			if (accountId!=null){
				uservip.setAccountId(accountId);
			}
			String loginName=optString("loginName",request);
			if (loginName!=null){
				uservip.setLoginName(loginName);
			}
			String userNo=optString("userNo",request);
			if (userNo!=null){
				uservip.setUserNo(userNo);
			}
			String sourceCode=optString("sourceCode",request);
			if (sourceCode!=null){
				uservip.setSourceCode(sourceCode);
			}
			Integer memberId=optInteger("memberId",request);
			if (memberId!=null){
				uservip.setMemberId(memberId);
			}
			uservip.setGmtModify(new Date());
			uservipService.saveOrUpdate(uservip);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除用户会员信息
		*/
		@ResponseBody
		@RequestMapping("delUservipByIds")
		public String delUservipByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Uservip> uservipList= uservipService.getByParams(params);
			if (uservipList==null||uservipList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Uservip uservip : uservipList) {
				uservip.setDelStatus(DelStatus.DEL);
			}
			uservipService.saveOrUpdateAll(uservipList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-34
		 * 查询会员列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getUservip")
		public String getUservip(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String gender=optString("gender", request);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			if (gender!=null&&gender.length()>0) {
				prams.put("gender", gender);
			}
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
				searchPram.put("nickname", pms);
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Uservip> uservipList=uservipService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (uservipList==null||uservipList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=uservipService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, uservipList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询用户会员信息
		*/
		@ResponseBody
		@RequestMapping("getUservipById")
		public String getUservipById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Uservip uservip=uservipService.get(id);
			if (uservip==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",uservip);//jsonObject	json对象
		}

		/**
		 * 查询所有用户会员信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllUservip")
		public String getAllUservip(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Uservip> uservipList= uservipService.getByParams(null);
			if (uservipList==null||uservipList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",uservipList);//jsonObjectList	json对象集合
		}
}
